﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.SessionState;

namespace BlueMarineWebClient.DO
{
    public class bookingcart
    {
        private readonly HttpSessionState Session;
        private readonly List<bookInfo> list;

        private bookingcart(HttpSessionState Session)
        {
            this.Session = Session;
            list = (List<bookInfo>) Session["listBooking"];
            if (list == null) list = new List<bookInfo>();
        }

        public double TotalAmount
        {
            get
            {
                IEnumerable<double> query = from bookInfo info in list
                                            select (info.Amount);
                return query.Sum();
            }
        }

        public ArrayList Items
        {
            get { return new ArrayList(list); }
        }

        public static bookingcart GetCart(HttpSessionState Session)
        {
            return new bookingcart(Session);
        }

        public void Add(bookInfo info)
        {
            //if (list.Contains(info))
            //{
            //    bookInfo CurrentInfo = (bookInfo)list[info.RoomID];

            //    list.Remove(info);
            //    return;
            //}
            //else
            //{
            //    list.Add(info);
            //}
            if (list.Count == 0)
            {
                list.Add(info);
                Session["listBooking"] = list;
                return;
            }

            foreach (bookInfo bookinfo in list)
            {
                if (bookinfo.RoomID == info.RoomID)
                {
                    return;
                }
                else
                {
                    list.Add(info);
                    Session["listBooking"] = list;
                    return;
                }
            }
        }

        public void Remove(int index)
        {
            list.RemoveAt(index);
            Session["listBooking"] = list;
        }
    }
}